package com.pig4cloud.pig.admin.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.admin.api.entity.PrRfidLogEntity;
import com.pig4cloud.pig.admin.service.PrRfidLogService;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.plugin.excel.annotation.RequestExcel;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 操作日志
 *
 * @author pig
 * @date 2025-06-08 20:53:00
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/prRfidLog" )
@Tag(description = "prRfidLog" , name = "操作日志管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class PrRfidLogController {

    private final  PrRfidLogService prRfidLogService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param prRfidLog 操作日志
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('admin_prRfidLog_view')" )
    public R getPrRfidLogPage(@ParameterObject Page page, @ParameterObject PrRfidLogEntity prRfidLog) {
        LambdaQueryWrapper<PrRfidLogEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(StringUtils.isNotBlank(prRfidLog.getOperateStatus()),PrRfidLogEntity::getOperateStatus,prRfidLog.getOperateStatus());
		wrapper.eq(StringUtils.isNotBlank(prRfidLog.getTakeStatus()),PrRfidLogEntity::getTakeStatus,prRfidLog.getTakeStatus());
		wrapper.like(StringUtils.isNotBlank(prRfidLog.getDeviceName()),PrRfidLogEntity::getDeviceName,prRfidLog.getDeviceName());
		wrapper.like(StringUtils.isNotBlank(prRfidLog.getRfidName()),PrRfidLogEntity::getRfidName,prRfidLog.getRfidName());
		wrapper.like(StringUtils.isNotBlank(prRfidLog.getCreateBy()),PrRfidLogEntity::getCreateBy,prRfidLog.getCreateBy());
		wrapper.orderByDesc(PrRfidLogEntity::getCreateTime);
        return R.ok(prRfidLogService.page(page, wrapper));
    }


    /**
     * 通过条件查询操作日志
     * @param prRfidLog 查询条件
     * @return R  对象列表
     */
    @Operation(summary = "通过条件查询" , description = "通过条件查询对象" )
    @GetMapping("/details" )
    @PreAuthorize("@pms.hasPermission('admin_prRfidLog_view')" )
    public R getDetails(@ParameterObject PrRfidLogEntity prRfidLog) {
        return R.ok(prRfidLogService.list(Wrappers.query(prRfidLog)));
    }

    /**
     * 新增操作日志
     * @param prRfidLog 操作日志
     * @return R
     */
    @Operation(summary = "新增操作日志" , description = "新增操作日志" )
    @SysLog("新增操作日志" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('admin_prRfidLog_add')" )
    public R save(@RequestBody PrRfidLogEntity prRfidLog) {
        return R.ok(prRfidLogService.save(prRfidLog));
    }

    /**
     * 修改操作日志
     * @param prRfidLog 操作日志
     * @return R
     */
    @Operation(summary = "修改操作日志" , description = "修改操作日志" )
    @SysLog("修改操作日志" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin_prRfidLog_edit')" )
    public R updateById(@RequestBody PrRfidLogEntity prRfidLog) {
        return R.ok(prRfidLogService.updateById(prRfidLog));
    }

    /**
     * 通过id删除操作日志
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除操作日志" , description = "通过id删除操作日志" )
    @SysLog("通过id删除操作日志" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('admin_prRfidLog_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(prRfidLogService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param prRfidLog 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('admin_prRfidLog_export')" )
    public List<PrRfidLogEntity> exportExcel(PrRfidLogEntity prRfidLog,Long[] ids) {
        return prRfidLogService.list(Wrappers.lambdaQuery(prRfidLog).in(ArrayUtil.isNotEmpty(ids), PrRfidLogEntity::getId, ids));
    }

    /**
     * 导入excel 表
     * @param prRfidLogList 对象实体列表
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('admin_prRfidLog_export')" )
    public R importExcel(@RequestExcel List<PrRfidLogEntity> prRfidLogList, BindingResult bindingResult) {
        return R.ok(prRfidLogService.saveBatch(prRfidLogList));
    }
}
